package com.cy.shiro.service.impl;

import com.cy.shiro.dao.SysRoleResourcesDao;
import com.cy.shiro.pojo.SysRoleResources;
import com.cy.shiro.service.SysRoleResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysRoleResourcesServiceImpl implements SysRoleResourcesService {

    @Autowired
    private SysRoleResourcesDao sysRoleResourcesDao;

    @Override
    public Integer delRole(Integer roleId) {
        return sysRoleResourcesDao.delRoleResource(roleId);
    }

    @Override
    public Integer addRoleResources(Integer roleId, String resourcesIds) {

        //删除 todo
        sysRoleResourcesDao.delRoleResource(roleId);


        //添加
        if(StringUtils.isEmpty(resourcesIds)){
           return null;
        }
        String[] resourcesArr = resourcesIds.split(",");
        if(resourcesArr.length == 0){
            return null;
        }
        SysRoleResources r = null;
        List<SysRoleResources> roleResources = new ArrayList<>();
        for(String ri : resourcesArr){
            if(StringUtils.isEmpty(ri)){
                continue;
            }
            r = new SysRoleResources();
            r.setRoleId(roleId);
            r.setResourcesId(Integer.valueOf(ri));
            r.setCreateTime(new Date());
            r.setUpdateTime(new Date());
            roleResources.add(r);
        }
        if(roleResources.size() == 0){
            return null;
        }
        //todo
        return sysRoleResourcesDao.insertList(roleResources);
    }


}
